#include "sqlite_manager.h"
#include <iostream>
#include <string>

int main() {
    // 获取SQLiteManager单例实例
    SQLiteManager& dbManager = SQLiteManager::getInstance();
    
    // 连接数据库
    if (!dbManager.connect("test_data_field.db")) {
        std::cerr << "无法连接到数据库" << std::endl;
        return 1;
    }
    
    std::cout << "成功连接到数据库" << std::endl;
    
    // 创建一个带有JSON数据的QSO记录
    QSORecord record;
    record.date = "2024-05-21";
    record.time = "10:30:00";
    record.callsign = "BA1AA";
    record.band = "20M";
    record.mode = "SSB";
    record.rst_sent = "59";
    record.rst_rcvd = "59";
    record.name = "张三";
    record.qth = "北京";
    record.memo = "测试记录";
    record.data = "{\"equipment\":\"FT-991A\",\"antenna\":\"DIPOLE\",\"power\":100,\"notes\":\"测试数据字段\"}";
    record.log_creation_date = "2024-05-21 10:30:00";
    record.record_create_time = "2024-05-21 10:30:00";
    record.record_modify_time = "2024-05-21 10:30:00";
    
    // 插入记录
    if (dbManager.insertQSO(record)) {
        std::cout << "成功插入带data字段的QSO记录" << std::endl;
    } else {
        std::cerr << "插入QSO记录失败" << std::endl;
        return 1;
    }
    
    // 查询所有记录并显示data字段
    auto records = dbManager.getAllQSOs();
    std::cout << "查询到 " << records.size() << " 条记录:" << std::endl;
    for (const auto& rec : records) {
        std::cout << "ID: " << rec.id << std::endl;
        std::cout << "Date: " << rec.date << std::endl;
        std::cout << "Time: " << rec.time << std::endl;
        std::cout << "Callsign: " << rec.callsign << std::endl;
        std::cout << "Data: " << rec.data << std::endl;
        std::cout << "---" << std::endl;
    }
    
    // 更新记录中的data字段
    if (!records.empty()) {
        QSORecord& updateRecord = const_cast<QSORecord&>(records[0]);
        updateRecord.data = "{\"equipment\":\"FT-991A\",\"antenna\":\"VERTICAL\",\"power\":50,\"notes\":\"更新后的测试数据\"}";
        updateRecord.record_modify_time = "2024-05-21 11:00:00";
        
        if (dbManager.updateQSO(updateRecord)) {
            std::cout << "成功更新记录的data字段" << std::endl;
        } else {
            std::cerr << "更新QSO记录失败" << std::endl;
            return 1;
        }
    }
    
    // 再次查询验证更新
    auto updatedRecords = dbManager.getAllQSOs();
    std::cout << "更新后查询到 " << updatedRecords.size() << " 条记录:" << std::endl;
    for (const auto& rec : updatedRecords) {
        std::cout << "ID: " << rec.id << std::endl;
        std::cout << "Date: " << rec.date << std::endl;
        std::cout << "Time: " << rec.time << std::endl;
        std::cout << "Callsign: " << rec.callsign << std::endl;
        std::cout << "Data: " << rec.data << std::endl;
        std::cout << "---" << std::endl;
    }
    
    std::cout << "data字段CRUD测试完成!" << std::endl;
    
    // 断开数据库连接
    dbManager.disconnect();
    
    return 0;
}